home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Original Tracker 3.10 Source / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-22  |  9.3 KB  |  410 lines  |  [TEXT/KAHL]

  1. /* read.c */
  2.  
  3. /* $Id: read.c,v 3.5 1992/11/27 10:29:00 espie Exp espie $
  4.  * $Log: read.c,v $
  5.  * Revision 3.5  1992/11/27  10:29:00  espie
  6.  * General cleanup
  7.  *
  8.  * Revision 3.4  1992/11/23  17:18:59  espie
  9.  * *** empty log message ***
  10.  *
  11.  * Revision 3.3  1992/11/23  10:12:23  espie
  12.  * *** empty log message ***
  13.  *
  14.  * Revision 3.2  1992/11/22  17:20:01  espie
  15.  * Checks for finetune ?
  16.  *
  17.  * Revision 3.1  1992/11/19  20:44:47  espie
  18.  * Protracker commands.
  19.  *
  20.  * Revision 3.0  1992/11/18  16:08:05  espie
  21.  * New release.
  22.  *
  23.  * Revision 2.16  1992/11/17  17:06:25  espie
  24.  * fix_xxx for better speed.
  25.  * Added some sample information in the dump.
  26.  * Added transpose feature.
  27.  * Feature fix: length 1 sample should be empty.
  28.  * Corrected repeat length problems concerning badly formed files,
  29.  * added signature checking for new tracker files.
  30.  * Corrected small problem with repeat being too short.
  31.  * Coded error types. More amiga specific stuff.
  32.  *
  33.  * Revision 1.17  1991/11/17  16:30:48  espie
  34.  * Rationnalized error recovery.
  35.  * There was a bug: you could try to deallocate
  36.  * stuff in no-noland. Also, strings never got
  37.  * to be freed.
  38.  * Centralized error control to error_song.
  39.  * Added a new test on length, aborts most modules now.
  40.  * Maybe should say it as well.
  41.  * Added checkpoints for early return if file too short.
  42.  * Added memory recovery and error control.
  43.  * Suppressed ! warning for bad note.
  44.  * Added note support.
  45.  * Corrected length and rep_length/rep_offset
  46.  * which are given in words and should be converted to
  47.  * bytes.
  48.  */
  49.  
  50. #include <malloc.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <ctype.h>
  54. #include <assert.h>
  55.  
  56. #include "defs.h"
  57. #include "extern.h"
  58. #include "song.h"
  59. #include "channel.h"
  60.  
  61. LOCAL char *id = "$Id: read.c,v 3.5 1992/11/27 10:29:00 espie Exp espie $";
  62.  
  63. /***
  64.  *
  65.  *    Low level st-file access routines 
  66.  *
  67.  ***/
  68.  
  69. /* c = checkgetc(f):
  70.  * gets a character from file f.
  71.  * Aborts program if file f is finished
  72.  */
  73. LOCAL int checkgetc(f)
  74. FILE *f;
  75.     {
  76.     int c;
  77.  
  78.     if ((c = fgetc(f)) == EOF)
  79.         error = FILE_TOO_SHORT;
  80.     return c;
  81.     }
  82.  
  83. #define MAX_LEN 50
  84. /* s = getstring(f, len):
  85.  * gets a soundtracker string from file f.
  86.  * I.e, it is a fixed length string terminated
  87.  * by a 0 if too short. Length should be
  88.  * smaller than MAX_LEN.
  89.  */
  90. LOCAL char *getstring(f, len)
  91. FILE *f;
  92. int len;
  93.     {
  94.     static char s[MAX_LEN];
  95.     char *new;
  96.     int i;
  97.         
  98.     assert(len < MAX_LEN);
  99.     for (i = 0; i < len; i++)
  100.         s[MIN(i, MAX_LEN - 1)] = checkgetc(f);
  101.     s[MIN(len, MAX_LEN - 1)] = '\0';
  102.     new = malloc(strlen(s)+1);
  103.  
  104.     return strcpy(new, s);
  105.     }
  106.  
  107. /* byteskip(f, len)
  108.  * same as fseek, xcpt it works on stdin
  109.  */
  110. LOCAL void byteskip(f, len)
  111. FILE *f;
  112. int len;
  113.     {
  114.     int i;
  115.  
  116.     for (i = 0; i < len; i++)
  117.         checkgetc(f);
  118.     }
  119.  
  120. /* v = getushort(f)
  121.  * reads an unsigned short from f
  122.  */
  123. LOCAL int getushort(f)
  124. FILE *f;
  125.     {
  126.     int i;
  127.  
  128.     i = checkgetc(f) << 8;
  129.     return i | checkgetc(f);
  130.     }
  131.  
  132.  
  133. /* fill_sample_info(info, f):
  134.      fill sample info with the information at current position of
  135.      file f. Allocate memory for storing the sample, also.
  136.     fill_sample_info is guaranteed to give you an accurate snapshot
  137.     of what sample should be like. In particular, length, rp_length,
  138.     start, rp_start, fix_length, fix_rp_length will have the values
  139.     you can expect if part of the sample is missing.
  140.  */
  141. LOCAL void fill_sample_info(info, f)
  142. struct sample_info *info;
  143. FILE *f;
  144.     {
  145.     info->name = getstring(f, SAMPLENAME_MAXLENGTH);
  146.     info->length = getushort(f);
  147.     info->finetune = checkgetc(f);
  148.     if (info->finetune > 15)
  149.         info->finetune = 0;
  150.     info->volume = checkgetc(f);
  151.     info->volume = MIN(info->volume, MAX_VOLUME);
  152.     info->rp_offset = getushort(f);
  153.     info->rp_length = getushort(f);
  154.  
  155.     /* the next check is for old modules for which
  156.      * the sample data types are a bit confused, so
  157.      * that what we were expecting to be #words is #bytes.
  158.      */
  159.         /* not sure I understand the -1 myself, though it's
  160.          * necessary to play kawai-k1 correctly 
  161.          */
  162.     if (info->rp_length + info->rp_offset - 1 > info->length)
  163.         info->rp_offset /= 2;
  164.     
  165.     if (info->rp_length + info->rp_offset > info->length)
  166.         info->rp_length = info->length - info->rp_offset;
  167.  
  168.     info->length *= 2;
  169.     info->rp_offset *= 2;
  170.     info->rp_length *= 2;
  171.         /* in all logic, a 2-sized sample could exist,
  172.          * but this is not the case, and even so, some
  173.          * trackers output empty instruments as being 2-sized.
  174.          */
  175.     if (info->length <= 2)
  176.         {
  177.         info->start = NULL;
  178.         info->length = 0;
  179.         }
  180.     else
  181.         {
  182.         info->start = (SAMPLE *)calloc(info->length, 1);
  183.  
  184.         if (info->rp_length > 2)
  185.             info->rp_start = info->start + info->rp_offset;
  186.         else
  187.             {
  188.             info->rp_start = NULL;
  189.             info->rp_length = 0;
  190.             }
  191.         }
  192.  
  193.     if (info->length > MAX_SAMPLE_LENGTH)
  194.         error = CORRUPT_FILE;
  195.     info->fix_length = int_to_fix(info->length);
  196.     info->fix_rp_length = int_to_fix(info->rp_length);
  197.     }
  198.  
  199. LOCAL void fill_song_info(info, f)
  200. struct song_info *info;
  201. FILE *f;
  202.     {
  203.     int i;
  204.     int p;
  205.  
  206.     info->length = checkgetc(f);
  207.     checkgetc(f);
  208.     info->maxpat = -1;
  209.     for (i = 0; i < NUMBER_PATTERNS; i++)
  210.         {
  211.         p = checkgetc(f);
  212.         if (p >= NUMBER_PATTERNS)
  213.             p = 0;
  214.         if (p > info->maxpat)
  215.             info->maxpat = p;
  216.         info->patnumber[i] = p;
  217.         }
  218.     info->maxpat++;
  219.     if (info->maxpat == 0 || info->length == 0)
  220.         error = CORRUPT_FILE;
  221.     }
  222.  
  223. LOCAL void fill_event(e, f)
  224. struct event *e;
  225. FILE *f;
  226.     {
  227.     int a, b, c, d;
  228.  
  229.     a = checkgetc(f);
  230.     b = checkgetc(f);
  231.     c = checkgetc(f);
  232.     d = checkgetc(f);
  233.     e->sample_number = (a & 0x10) | (c >> 4);
  234.     e->effect = c & 0xf;
  235.     e->parameters = d;
  236.     e->pitch = ( (a & 15) << 8 ) | b;
  237.     e->note = find_note(e->pitch);
  238.     }
  239.  
  240. LOCAL void fill_pattern(pattern, f)
  241. struct block *pattern;
  242. FILE *f;
  243.     {
  244.     int i, j;
  245.  
  246.     for (i = 0; i < BLOCK_LENGTH; i++)
  247.         for (j = 0; j < NUMBER_TRACKS; j++)
  248.             fill_event(&(pattern->e[j][i]), f);
  249.     }
  250.  
  251.  
  252. LOCAL void read_sample(info, f)
  253. struct sample_info *info;
  254. FILE *f;
  255.     {
  256.  
  257.     if (info->start)
  258.         {
  259.         fread(info->start, 1, info->length, f);
  260.         }
  261.     }
  262.  
  263.  
  264.  
  265.  
  266. /***
  267.  *
  268.  *  new_song: allocates a new structure for a song.
  269.  *  clears each and every field as appropriate.
  270.  *
  271.  ***/
  272. LOCAL struct song *new_song()
  273.     {
  274.     struct song *new;
  275.     int i;
  276.  
  277.     new = (struct song *)malloc(sizeof(struct song));
  278.     new->title = NULL;
  279.     new->info.length = 0;
  280.     new->info.maxpat = -1;
  281.     new->info.transpose = 0;
  282.     new->info.pblocks = NULL;
  283.     for (i = 0; i < NUMBER_SAMPLES; i++)
  284.         {
  285.         new->samples[i].finetune = 0;
  286.         new->samples[i].name = NULL;
  287.         new->samples[i].length = NULL;
  288.         new->samples[i].start = NULL;
  289.         new->samples[i].rp_start = NULL;
  290.         }
  291.     return new;
  292.     }
  293.  
  294. /* release_song(song): gives back all memory 
  295.  * occupied by song. Assume that each structure
  296.  * has been correctly allocated by a call to the
  297.  * corresponding new_XXX function.
  298.  */
  299. void release_song(song)
  300. struct song *song;
  301.     {
  302.     int i;
  303.  
  304.     for (i = 0; i < NUMBER_SAMPLES; i++)
  305.         {
  306.         if (song->samples[i].start)
  307.             free(song->samples[i].start);
  308.         if (song->samples[i].name)
  309.             free(song->samples[i].name);
  310.         }
  311.     if (song->info.pblocks)
  312.         free(song->info.pblocks);
  313.     if (song->title)
  314.         free(song->title);
  315.     free(song);
  316.     }
  317.  
  318. /* error_song(song): what we should return
  319.  * if there was an error. Actually, is mostly
  320.  * useful for its side effects.
  321.  */
  322. LOCAL struct song *error_song(song)
  323. struct song *song;
  324.     {
  325.     release_song(song);
  326.     return NULL;
  327.     }
  328.  
  329. /* bad_sig(f): read the signature on file f
  330.  * and returns TRUE if it is not a known sig.
  331.  */
  332. LOCAL BOOL bad_sig(f)
  333. FILE *f;
  334.     {
  335.     char a, b, c, d;
  336.  
  337.     a = checkgetc(f);
  338.     b = checkgetc(f);
  339.     c = checkgetc(f);
  340.     d = checkgetc(f);
  341.     if (a == 'M' && b == '.' && c == 'K' && d == '.')
  342.         return FALSE;
  343.     if (a == 'M' && b == '&' && c == 'K' && d == '!')
  344.         return FALSE;
  345.     if (a == 'F' && b == 'L' && c == 'T' && d == '4')
  346.         return FALSE;
  347.     return TRUE;
  348.     }
  349.  
  350. /* s = read_song(f, type): tries to read a song s
  351.  * of type NEW/OLD in file f. Might fail, i.e.,
  352.  * returns NULL if file is not a mod file of the
  353.  * correct type.
  354.  */
  355. struct song *read_song(f, type)
  356. FILE *f;
  357. int type;
  358.     {
  359.     struct song *song;
  360.     int i;
  361.     int ninstr;
  362.  
  363.     error = NONE;
  364.     if (type == NEW || type == NEW_NO_CHECK)
  365.         ninstr = 31;
  366.     else
  367.         ninstr = 15;
  368.  
  369.     song = new_song();
  370.     song->title = getstring(f, TITLE_MAXLENGTH);
  371.     if (error != NONE)
  372.         return error_song(song);
  373.  
  374.     for (i = 1; i <= ninstr; i++)
  375.         {
  376.         fill_sample_info(&song->samples[i], f);
  377.         if (error != NONE)
  378.             return error_song(song);
  379.         }
  380.  
  381.     fill_song_info(&song->info, f);
  382.  
  383.     if (error != NONE)
  384.         return error_song(song);
  385.  
  386.     if (type == NEW && bad_sig(f))
  387.         return error_song(song);
  388.  
  389.     if (type == NEW_NO_CHECK)
  390.         byteskip(f, 4);
  391.         
  392.  
  393.     song->info.pblocks = (struct block *)
  394.         malloc(sizeof(struct block) * song->info.maxpat);
  395.     for (i = 0; i < song->info.maxpat; i++)
  396.         {
  397.         fill_pattern(song->info.pblocks + i, f);
  398.         if (error != NONE)
  399.             return error_song(song);
  400.         }
  401.  
  402.     for (i = 1; i <= ninstr; i++)
  403.         read_sample(&song->samples[i], f);
  404.     
  405.     if (error != NONE)
  406.         return error_song(song);
  407.     return song;
  408.     }
  409.  
  410.